<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>HTML 5拖拽</title>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }

    body {
      background: linear-gradient(135deg, #6e8efb, #a777e3);
      color: #333;
      min-height: 100vh;
      padding: 20px;
    }

    .container {
      max-width: 1200px;
      margin: 0 auto;
      display: flex;
      justify-content: space-around;
    }

    .left {
      width: 500px;
      height: 500px;
      border: 1px solid red;
      display: flex;
      justify-content: space-around;
      padding: 20px 0;
    }
    .right {
      width: 500px;
      height: 500px;
      border: 1px solid red;
       display: flex;
      justify-content: space-around;
      padding: 20px 0;
    }
    .box {
      width: 100px;
      height: 40px;
      border: 1px solid green;
      border-radius: 4px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="left">
      <div class="box" draggable="true" data-id="1"></div>
      <div class="box" draggable="true" data-id="2"></div>
      <div class="box" draggable="true" data-id="3"></div>
    </div>
    <div class="right"></div>
  </div>

  <script>
    const draggableItems = document.querySelectorAll('.box')
    const dropZone = document.querySelector('.right')

    draggableItems.forEach(item => {
      item.addEventListener('dragstart', handleDragStart)
      item.addEventListener('dragend', handlerDragEnd)
    })


    function handleDragStart(e) {
      console.log(e)
      e.dataTransfer.setData('text/plain', e.target.dataset.id)
      e.dataTransfer.effectAllowed = 'move'
    }

    function handlerDragEnd(e) {}
    dropZone.addEventListener('dragover', handleDragOver);
    dropZone.addEventListener('dragenter', handleDragEnter);
    dropZone.addEventListener('dragleave', handleDragLeave);
    dropZone.addEventListener('drop', handleDrop);

    function handleDragOver(e) {
      // 阻止默认行为以允许放置
      e.preventDefault();
    }

    function handleDragEnter(e) {
      // 添加高亮样式
      // e.preventDefault();
    }

    function handleDragLeave(e) {
        // 移除高亮样式
    }
    
    function handleDrop(e) {
      e.preventDefault()
      const id = e.dataTransfer.getData('text/plain')
      const draggedItem = document.querySelector(`.box[data-id="${id}"]`)
      if (draggedItem && !dropZone.contains(draggedItem)) {
        const clonedItem = draggedItem.cloneNode(true)
        clonedItem.draggable = false 

        dropZone.appendChild(clonedItem)
      }
    }
  </script>
</body>
</html>